package com.situ.springboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.UserMapper;
import com.situ.springboot.pojo.User;
import com.situ.springboot.pojo.query.UserQuery;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// Alt+Enter
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    //在Service层封装分页需要的LayUITableResult
    @Override
    public LayUITableResult list(UserQuery userQuery) {
        //使用PageHelper开启分页
        PageHelper.startPage(userQuery.getPage(), userQuery.getLimit());
        //开启分页之后，查找所有会自动带上分页
        List<User> list = userMapper.list(userQuery);
        //获取总的数量，目的是为了算分页
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        return LayUITableResult.ok((int)totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            userMapper.deleteById(id);
        }
    }

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public User login(String name, String password) {
        return userMapper.login(name, password);
    }

    /*@Override
    public List<User> list() {
        List<User> list = userMapper.list();
        for (User user : list) {
            user.setName("situ-" + user.getName());
        }
        return list;

        //return userMapper.list();
    }*/
}
